<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="项目编号" prop="grantId">
        <el-select v-model="queryParams.grantId" placeholder="请选择项目类型">
          <el-option
            v-for="dict in grantOptions"
            :key="dict.grantId"
            :label="dict.grantName"
            :value="dict.grantId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['grant:grcompany:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="grcompanyList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="单位名称" align="center" prop="comName" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="是否启用" align="center" prop="isEnabled" />
      <el-table-column label="项目" align="center" prop="grantId" />
<!--      <el-table-column label="计划数量" align="center" prop="plannedQuantity" />-->
      <el-table-column label="级别" align="center" prop="lvl" />
      <el-table-column label="报请清单盖章件" align="center">
        <template slot-scope="scope">
          <el-link
            type="text"
            @click="downloadConfirmfile(scope.row)"
          >{{scope.row.confirmFileurl}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['grant:grcompany:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['grant:grcompany:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改派出单位管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位名称" prop="comName">
          <el-input v-model="form.comName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="是否可用" prop="isEnabled">
          <el-select v-model="form.isEnabled" placeholder="请选择是否启用">
            <el-option
              v-for="dict in yesnoOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="上级单位" prop="parentId">
          <treeselect v-model="form.parentId" :options="grcompanyOptions" :normalizer="normalizer" placeholder="请选择上级单位" />
        </el-form-item>
        <el-form-item label="项目" prop="grantId">
          <el-select v-model="form.grantId" placeholder="请选择项目">
            <el-option
              v-for="dict in grantOptions"
              :key="dict.grantId"
              :label="dict.grantName"
              :value="dict.grantId"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="计划数量" prop="plannedQuantity">-->
<!--          <el-input v-model="form.plannedQuantity" placeholder="请输入计划数量" />-->
<!--        </el-form-item>-->
        <el-form-item label="级别" prop="lvl">
          <el-input v-model="form.lvl" placeholder="级别" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGrcompany,
  getGrcompany,
  delGrcompany,
  addGrcompany,
  updateGrcompany,
  exportGrcompany,
  downloadConfirmfile,
} from "@/api/grant/grcompany";
import { listGrantAll } from "@/api/grant/grant"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Grcompany",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 派出单位管理表格数据
      grcompanyList: [],
      // 派出单位管理树选项
      grcompanyOptions: [],
      grantOptions: [],
      yesnoOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        comCode: undefined,
        comName: undefined,
        granttypeId: undefined,
        createUser: undefined,
        isEnabled: undefined,
        parentId: undefined,
        grantId: undefined,
        plannedQuantity: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        comName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" }
        ],
        grantId: [
          { required: true, message: "项目不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    let query ={};
    query["isEnabled"]="0";
    listGrantAll(query).then(res=>{
      this.grantOptions = res.data;
      this.queryParams.grantId=res.data[res.data.length-1].grantId;
      this.getList();
    })
    this.getDicts("sys_yes_no").then(response => {
      this.yesnoOptions = response.data;
    });
  },
  methods: {
    /** 查询派出单位管理列表 */
    getList() {
      this.loading = true;
      listGrcompany(this.queryParams).then(response => {
        this.grcompanyList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 转换派出单位管理数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.comName,
        children: node.children
      };
    },
	/** 查询派出单位下拉树结构 */
    getTreeselect() {

      listGrcompany(this.queryParams).then(response => {
        this.grcompanyOptions = [];
        const data = { id: 0, comName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.grcompanyOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        comCode: undefined,
        comName: undefined,
        granttypeId: undefined,
        createTime: undefined,
        createUser: undefined,
        isEnabled: undefined,
        parentId: undefined,
        grantId: undefined,
        plannedQuantity: 0,
        lvl: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
	  this.getTreeselect();
	  this.form.grantId = this.queryParams.grantId;
      this.open = true;
      this.title = "添加派出单位管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
	  this.getTreeselect();
      if (row != undefined) {
        this.form.parentId = row.id;
      }
      getGrcompany(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改派出单位管理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateGrcompany(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addGrcompany(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除派出单位管理编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGrcompany(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    downloadConfirmfile(row){
      let query = {};
      query['confirmFileurl'] = row.confirmFileurl;
      downloadConfirmfile(query).then(res=>{
        // this.download(res.msg);
        window.open(res.msg, '_blank', 'noopener,noreferrer');
      });
    }
  }
};
</script>
